
# Common build process for effect and synth units. These only differ in the
# architecture which is being used.

from os.path import splitext, basename, dirname

def mybuild(bld, arch):
    # all .dsp sources in this directory
    sources = bld.path.ant_glob('*.dsp')
    for x in sources:
        # get the basename of the module
        y, _ = splitext(basename(x.relpath()))
        # determine the plugin dir and the installation path
        if bld.env.split_bundles:
            plugindir = y+".lv2"
            lv2_install_path = (None if bld.env.no_plugin_install
                                else '%s/%s' % (bld.options.lv2dir, plugindir))
        else:
            plugindir = bld.env.plugindir
            lv2_install_path = bld.env.lv2_install_path
        # compile the Faust source
        bld(rule="faust -a ../%s -cn %s ${SRC} -o ${TGT}" % (arch, y),
            source=y+'.dsp', target=y+'.cpp')
        bld.add_manual_dependency(bld.path.find_resource(y+'.dsp'),
                                  bld.path.parent.find_resource(arch))
        # compile the resulting C++ source to a shared module
        obj = bld.shlib(source=y+'.cpp',
                        target="../lv2/%s/%s" % (plugindir, y),
                        install_path = lv2_install_path,
                        includes='..', defines=bld.env.mydefines)
        # dirty hack to fix up the shared module name; waf doesn't seem to
        # know about dlopen modules
        _, dllext = splitext(obj.env.cxxshlib_PATTERN)
        obj.env.cxxshlib_PATTERN = '%s'+dllext
        # compile the module executable if needed
        if not bld.env.dyn_manifest:
            prog = bld.program(source=y+'.cpp', target=y+'-manifest',
                               install_path = None, includes='..',
                               defines=bld.env.mydefines)
        template = ('../lv2-manifest-template.ttl' if bld.env.dyn_manifest
                    else '../lv2-manifest-static-template.ttl')
        manifest = ('../lv2/%s/manifest.ttl' % plugindir
                    if bld.env.split_bundles else '%s-manifest.ttl' % y)
        bld(features='subst',
            source = template,
            target = manifest,
            name = y, uri = '%s/%s' % (bld.options.uri_prefix, y),
            dllext = dllext,
            install_path = lv2_install_path if bld.env.split_bundles else None)
        if not bld.env.dyn_manifest:
            bld(rule = '${SRC} > ${TGT}',
                source = y+'-manifest',
                target = '../lv2/%s/%s.ttl' % (plugindir, y),
                after = 'cxxprogram',
                install_path = lv2_install_path)
